﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.BaseInfo;

namespace VehicleMaintenance.Query
{
	public partial class SalesQueryForm : BaseForm
	{
		List<SalesBill> list;
		public SalesQueryForm()
		{
			InitializeComponent();

			ucQueryList1.InitializeColumnHeader(typeof(Models.SalesBill));
			ucQueryList1.dgvData.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvData_CellFormatting);
		}

		private void RecipientQueryForm_Load(object sender, EventArgs e)
		{
			try
			{
				cboxKind.SelectedIndex = 0;
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void dgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			try
			{
				string str = ucQueryList1.dgvData.Columns[e.ColumnIndex].DataPropertyName;
				if (str == "GoodID")
				{
					e.Value = list[e.RowIndex].Good.Code;
				}
				else if (str == "CarTypeID")
				{
					e.Value = list[e.RowIndex].CarType.Name;
				}
				else if (str == "StaffID")
				{
					e.Value = list[e.RowIndex].Staff.Name;
				}
				else if (str == "CustomerID")
				{
					e.Value = list[e.RowIndex].Customer.Name;
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnQuery_Click(object sender, EventArgs e)
		{
			BindData();
		}

		private void BindData()
		{
			try
			{
				CarDBContext db = new CarDBContext();
				if (cboxKind.Text.Equals("根据零件类别"))
				{
					if (txtValue.Text.Trim().Length == 0)
					{
						this.ShowInfo("请选择零件类别"); ;
						txtValue.Focus();
						return;
					}
					int goodid = Convert.ToInt32(txtValue.Tag);
					list = db.SalesBill.Include("Customer").Include("Good").Include("Staff").Include("CarType").Where(p => p.GoodID == goodid).ToList();
					ucQueryList1.dgvData.DataSource = list;
				}
				//else if (cboxKind.Text.Equals("根据车牌号"))
				//{
				//    CarDBContext db = new CarDBContext();
				//    list = db.SalesBill.SqlQuery(@"select a.*,c.*,d.*,e.* from SalesBill a,goods c,CarType d,staff e where (a.mainbillcode in (select b.mainbillcode from mainbill b where b.CarNo = @carno)
				//                                      and a.goodID=c.ID and a.cartypeid=d.cartypeid and a.staffid=e.staffid )", new MySql.Data.MySqlClient.MySqlParameter("@carno", txtValue.Text.Trim())).ToList();
				//    ucQueryList1.dgvData.DataSource = list;
				//}
				else
				{
					list = db.SalesBill.Include("Customer").Include("Good").Include("CarType").Include("Staff").ToList();
					ucQueryList1.dgvData.DataSource = list;
				}

				int num = 0;
				decimal total = 0;
				foreach (var item in list)
				{
					num += item.Number;
					total += item.SalesAmount;
				}
				lbNumber.Text = num.ToString();
				lbTotalPrice.Text = total.ToString() + "元";
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void txtValue_Click(object sender, EventArgs e)
		{
			try
			{

				TextBox txt = (sender as TextBox);

				GoodListForm form = new GoodListForm();
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.Good.Name;
					txt.Tag = form.Good.GoodID;					
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
	}
}
